public class trappingWater {
    public static void main(String[] args) {

        int[] testCase = {0,1,0,2,1,0,1,3,2,1,2,1};
        System.out.println("双指针结果: " + trapTwoPoint(testCase));
    }
    //积水的厚度是由矮墙决定的，这样我们可以默认右侧墙比左侧墙高，左侧高墙减低墙之和是容器
    //左侧的墙比右侧的墙高，这样相减，墙高是容积
    public static int trapTwoPointer(int[] height) {
        int left = 0, right = height.length - 1;
        int leftMax = 0, rightMax = 0;
        int res = 0;

        while (left < right) {
            if (height[left] < height[right]) {
                leftMax = Math.max(leftMax, height[left]);
                res += leftMax - height[left];
                left++;
            } else {
                rightMax = Math.max(rightMax, height[right]);
                res += rightMax - height[right];
                right--;
            }
        }
        return res;
    }

    public static int trapTwoPoint(int[] height) {
        int left = 0,right = height.length-1,leftMax=0,rightMax=0;
        int res =0;
        while (left< right){
            if (height[left]<height[right]){
                leftMax =Math.max(height[left],leftMax);
                res+=leftMax-height[left];
                left++;
            }else {
                rightMax =Math.max(height[right],leftMax);
                res+=rightMax-height[right];
                right--;
            }
        }
        return res;
    }
}
